package com.findmeastore.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class AuthFilter implements Filter{
	static final Logger logger = Logger.getLogger(AuthFilter.class);
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		try {
			Boolean authorized = false;
			if (request instanceof HttpServletRequest) {
				HttpServletRequest httpServletRequest = (HttpServletRequest) request;
				if (checkIfURIToBeSkipped(httpServletRequest.getRequestURI())) {
					filterChain.doFilter(request, response);
					return;
				} else {
					HttpSession session = httpServletRequest.getSession(false);
					if (session != null) {
						Object obj = session.getAttribute("logged-in");

						if (obj != null){
							authorized = (Boolean)obj;
						}
					}
				}
			}
			setActiveTabInSession(request);
			if (authorized) {
				filterChain.doFilter(request, response);
				return;
			} else {
				HttpServletResponse httpServletResponse = (HttpServletResponse) response;
				httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL("login.jsp"));

			}
		} catch (IOException io) {
			logger.error("IOException raised in AuthenticationFilter");
		} catch (ServletException se) {
			logger.error("ServletException raised in AuthenticationFilter");
		}
		logger.error("Authentication: Response dispatched ...");
	}
	
	private void setActiveTabInSession(ServletRequest request){
		String uri = ((HttpServletRequest) request).getRequestURI();
		String active = "";
		if(uri.toLowerCase().contains("offer")){
			active = "offer";
		}else if(uri.toLowerCase().contains("brand")){
			active = "brand";
		}else if(uri.toLowerCase().contains("product")){
			active = "product";
		}else if(uri.toLowerCase().contains("locality")){
			active = "locality";
		}else if(uri.toLowerCase().contains("business")){
			active = "business";
		}else if(uri.toLowerCase().contains("user")){
			active = "user";
		}else if(uri.toLowerCase().contains("category")){
			active = "category";
		}
		request.setAttribute("active", active);
	}
	
	private boolean checkIfURIToBeSkipped(String uri){
		if(uri.endsWith("login.jsp")){
			return true;
		}
		if(uri.endsWith("Login.html")){
			return true;
		}
		if(uri.endsWith(".css")){
			return true;
		}
		if(uri.endsWith(".js")){
			return true;
		}
		if(uri.endsWith(".png")){
			return true;
		}
		if(uri.endsWith(".jpg")){
			return true;
		}
		if(uri.endsWith(".gif")){
			return true;
		}
		return false;
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
